package team.wuyou.util.jdbc;

import java.sql.*;
import java.util.*;

public class Executeupdata {
    public static void main(String[] args) throws Exception {


        //update student set sname = '张三' , sage= , ssex = '', snativeplace = '', smajor='' , sclass= '' , snative = '' where sname='李四' and sid=10024
//        String sql = "UPDATE planInfo set remark= '"+remark+"'  where 1=1 And id= "+id+" And state=1";
//        executeupdata( sql);
//        String id="0078251515cb486fa7340da70bd59866,1";
//        //String id2="1";
//        Map map = new HashMap();
//        map.put("state",0);
//        Map map2 = new HashMap();
//        map2.put("state",1);
//        Map map3 = new HashMap();
//        map3.put("id",id);
//        updatas("user",map,map2,map3);
        HashMap map = new HashMap();
        map.put("username","bbb");
        map.put("password","bbb");
        map.put("name","bbb");
        map.put("imgPath","bbb");
        map.put("state","1");
        insert("user",map);
    }
/**
* @Description //TODO
* @Param person\wang\jdbc\Executeupdata.java
* @return int
* @Date 2023/11/9 10:22
* @Wang
* 这是个修改工具类
 *
 *
**/

static {
    try {
        Class.forName(Propertity.Driver);
    } catch (ClassNotFoundException e) {
        throw new RuntimeException(e);
    }
}
    public static int executeupdata(String sql) throws Exception {
        //String path = "jdbc:mysql://localhost:3306/some_test";
        //加载驱动
        //Class.forName("com.mysql.jdbc.Driver");
        //获取链接
        Connection conn = DriverManager.getConnection(Propertity.url, Propertity.username, Propertity.password);
        //载体
        Statement statement = conn.createStatement();
        System.out.println(sql);
        //执行sql
        int rs = statement.executeUpdate(sql);
        //被影响的行数为
        System.out.println("被影响的行数为："+rs);
        if (rs>0){
            System.out.println("成功");
        }else {
            System.out.println("失败");
        }
        return rs;
    }

/**
 * @return int
 * @Description //TODO
 * @Param person\wang\jdbc\Executeupdata.java
 * @Date 2023/11/13 10:36
 * @Wang
 * map1为修改参数
 * map2为查询参数
 **/


    public static int updata(String TableName, Map map, Map map2) throws Exception{
        List list = new ArrayList();
        List list2 = new ArrayList();
        Connection conn = DriverManager.getConnection(Propertity.url, Propertity.username, Propertity.password);

        //更改内容
        Set<Map.Entry<String,Object>> set = map.entrySet();
        Iterator<String> iterator = map.keySet().iterator();
        String next = iterator.next();
        String sql = "update "+TableName+" set `"+next+"` = `"+next+"` ";

        if (map.size()>1) {
            for (Map.Entry entry : set
            ) {
                if (entry.getKey()!=""&&entry.getKey()!=null&&entry.getValue()!=null&&entry.getValue()!="") {
                int i=1;

                sql = sql +", " + entry.getKey() + " = ?  ";
                list.add(entry.getValue());
                }
            }
            sql += "where 1=1";
        }else if (map.size()>0){
            for (Map.Entry entry : set)
            {
                sql = sql +" , "+ entry.getKey() + " = ? ";
                list.add(entry.getValue());
            }
            sql += "where 1=1";
        }

        //查询条件
        Set<Map.Entry<String,Object>> set2 = map2.entrySet();
        for (Map.Entry entry2: set2) {
            if (entry2.getKey()!=""&&entry2.getKey()!=null&&entry2.getValue()!=null&&entry2.getValue()!="") {
                sql += " AND " + entry2.getKey() + " = ? ";
                list.add(entry2.getValue());
            }
        }
        System.out.println(sql);
        PreparedStatement ps = conn.prepareStatement(sql);

        //将值付给 ？
        setData(ps,list);
//        ps.setObject(1,list.get(0));
//        ps.setObject(2,list2.get(0));
        //执行sql
        int rs = ps.executeUpdate();

        //被影响的行数为
        System.out.println("被影响的行数为："+rs);
        if (rs>0){
            System.out.println("成功");
        }else {
            System.out.println("失败");
        }
        return rs;
    }
    public static int updatas(String TableName, Map map, Map map2,Map map3) throws Exception{
        List list = new ArrayList();
        List list2 = new ArrayList();
        Connection conn = DriverManager.getConnection(Propertity.url, Propertity.username, Propertity.password);

        //更改内容
        Set<Map.Entry<String,Object>> set = map.entrySet();
        Iterator<String> iterator = map.keySet().iterator();
        String next = iterator.next();
        String sql = "update "+TableName+" set `"+next+"` = `"+next+"` ";

        if (map.size()>0) {
            for (Map.Entry entry : set) {
                int i=1;
                sql = sql +", " + entry.getKey() + " = ?  ";
                list.add(entry.getValue());
            }
            sql += "where 1=1";
        }
//        String sql = "UPDATE planInfo set remark= '"+remark+"'  where 1=1 And id= "+id+" And state=1";
        //UPDATE planInfo set remark= '"+remark+"'  where 1=1 And id in( "+id+") And state=1";
        //查询条件
        Set<Map.Entry<String,Object>> set2 = map2.entrySet();
        for (Map.Entry entry2: set2) {
            if (entry2.getKey()!=""&&entry2.getKey()!=null&&entry2.getValue()!=null&&entry2.getValue()!="") {
                sql += " AND " + entry2.getKey() + " = ? ";
                list.add(entry2.getValue());
            }
        }
        Set<Map.Entry<String,Object>> set3 = map3.entrySet();
        for (Map.Entry entry3: set3) {
            if (entry3.getKey()!=""&&entry3.getKey()!=null&&entry3.getValue()!=null&&entry3.getValue()!="") {
                String[] value = entry3.getValue().toString().split(",");
                sql+=" And "+ entry3.getKey() +" in ( ? ";
                list.add(value[0]);
                for (int i = 1; i <value.length ; i++) {
                    sql+=", ? ";
                    list.add(value[i]);
                }
                sql+=   ""+" );";
            }
        }
         //sql=sql+" And "+ entry3.getKey() +" in ( ? )";
        System.out.println(sql);
        PreparedStatement ps = conn.prepareStatement(sql);
        //将值付给 ？
        setData(ps,list);
        //执行sql
        int rs = ps.executeUpdate();
        //被影响的行数为
        System.out.println("被影响的行数为："+rs);
        if (rs>0){
            System.out.println("成功");
        }else {
            System.out.println("失败");
        }
        return rs;
    }

    public static int insert(String TableName, Map map) throws Exception{
        List list = new ArrayList();
        List list2 = new ArrayList();
        Connection conn = DriverManager.getConnection(Propertity.url, Propertity.username, Propertity.password);

        //更改内容
        Set<Map.Entry<String,Object>> set = map.entrySet();

        String sql = "insert into "+TableName+" ( ";

        for (Map.Entry entry: set) {
            if (entry.getKey()!=""&&entry.getKey()!=null&&entry.getValue()!=null&&entry.getValue()!="") {

                sql +=  entry.getKey() + " , ";
                list.add(entry.getValue());
            }
        }
        sql = sql.substring(0, sql.length() - 2)+" ) values (";
        System.out.println(sql);
        for (int i = 0; i < map.size(); i++) {
            sql+= " ? , ";
        }
        sql = sql.substring(0, sql.length() - 2);
        sql+=")";
        System.out.println(sql);
        PreparedStatement ps = conn.prepareStatement(sql);

        //将值付给 ？
        setData(ps,list);
//        ps.setObject(1,list.get(0));
//        ps.setObject(2,list2.get(0));
        //执行sql
        int rs = ps.executeUpdate();

        //被影响的行数为
        System.out.println("被影响的行数为："+rs);
        if (rs>0){
            System.out.println("成功");
        }else {
            System.out.println("失败");
        }
        return rs;
    }
    public static void setData(PreparedStatement ps,List list) throws SQLException {
        //System.out.println(list.toString());
        //System.out.println(list.size());
        for (int i = 0; i < list.size(); i++) {
           ps.setObject(i + 1, list.get(i));
           //System.out.println(list.get(i));
        }
    }
}